import {login, logout, getInfo} from '@/api/login'
import {getToken, setToken, removeToken} from '@/utils/auth'
import store from "../index";
import {userNoLogin} from '../../api/user'

const user = {
    state: {
        token: getToken(),
        name: '',
        organ: '',
        roles: [],
        ranges: []
    },

    mutations: {//用来定义对共享数据的修改方法
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_NAME: (state, name) => {
            state.name = name
        },
        SET_ORGAN: (state, organ) => {
            state.organ = organ
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
        },
        SET_RANGES: (state, ranges) => {
            state.ranges = ranges
        }
    },

    actions: {
        // 登录
        Login({commit}, userInfo) {
            const account = userInfo.account.trim()
            const password = userInfo.password.trim()
            return new Promise((resolve, reject) => {
                login(account, password).then(response => {
                    const data = response.data
                    console.log('data',data)
                    let token =  response.data.data.token;
                    if (data.code === 200) {
                        commit('SET_TOKEN', token) //调用vuex中的SET_TOKEN方法，修改token
                        setToken(token)//将token存储在cookie中
                        console.log("token:" + this.getters.token)
                    }
                    resolve(response)
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 免登录
        NoLogin({commit}, token) {
            return new Promise((resolve, reject) => {
                const param = {
                    token: token
                }
                userNoLogin(param).then(response => {
                    if(response.data.code === 997){
                        resolve(response)
                    }
                    if(response.data.code === 200){
                        let token =  response.data.data.token;
                        commit('SET_TOKEN', token) //调用vuex中的SET_TOKEN方法，修改token
                        setToken(token)//将token存储在cookie中
                        console.log("token:" + this.getters.token)
                        resolve(response)
                    }
                    console.log('data',data)
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 获取用户信息
        GetUserInfo({commit, state}) {
            return new Promise((resolve, reject) => {
                let response = {
                    data: {
                        roles: [],
                        ranges: null,
                        organCode: null,
                        organName: null,
                        name: null,
                    }
                }
                const category = localStorage.getItem('category')
                response.data.roles.push(category)
                const data = response.data
                commit('SET_ROLES', data.roles)
                commit('SET_NAME', data.name)//当前登录人
                localStorage.setItem('supplier_username', data.name)
                localStorage.setItem('supplier_code', data.organCode)
                localStorage.setItem('supplier_organ', data.organName)
                commit('SET_ORGAN', data.organName)//当前登录人所属供应商、商户、分销商名称
                commit('SET_RANGES', data.ranges)//当前登录账户
                resolve(response)
                /*getInfo(state.token).then(response => {
                    const data = response.data
                    console.log('getInfo', data)
                    if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
                        commit('SET_ROLES', data.roles)
                    } else {
                        console.error('getInfo: roles must be a non-null array !')
                    }
                    commit('SET_NAME', data.name)//当前登录人
                    localStorage.setItem('supplier_username', data.name)
                    localStorage.setItem('supplier_code',data.organCode)
                    localStorage.setItem('supplier_organ', data.organName)
                    commit('SET_ORGAN', data.organName)//当前登录人所属供应商、商户、分销商名称
                    commit('SET_RANGES', data.ranges)//当前登录账户
                    resolve(response)
                }).catch(error => {
                    reject(error)
                })*/
            })
        },

        // 登出
        LogOut({commit, state}) {
            return new Promise((resolve, reject) => {
                logout(state.token).then(() => {
                    commit('SET_TOKEN', '')
                    commit('SET_ROLES', [])
                    removeToken()
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 前端 登出
        FedLogOut({commit}) {
            return new Promise(resolve => {
                commit('SET_TOKEN', '')
                removeToken()
                resolve()
            })
        }
    }
}

export default user
